class Task
  constructor: ->
    queries = {}
    queries.page = 1
    queries.location = "全部"
    $.each document.location.search.substr(1).split('&'), (c,q)->
      i = q.split('=')
      if i[1]
        queries[i[0].toString()] = i[1].toString()
    @_id = window.location.pathname.split('/')[2]
    @params =
      current_page: queries.page
      location:     queries.location
    @selected = []

  add_comment_tweet: (tweet) ->
    push_flag = true
    for _t in @selected
      push_flag = false if _t.wid == tweet.wid || (_t.uid == tweet.uid && tweet.uid != "" )
    @selected.push(tweet) if push_flag

  remove_comment_tweet: (tweet) ->
    _tweets = []
    for _t in @selected
      _tweets.push _t unless _t.wid == tweet.wid ||(_t.uid == tweet.uid && _t.uid.toString() != 'undefined')
    @selected = _tweets

  recomment: () ->
    $.ajax
      url: "/s"
      type: "POST"
      data:
        comment: ""
        tweets:  @selected
        task:    @_id
        retweet: true
      success: (retval) =>
        console.log "success comment_weibos"
        for tweet in @selected
          $("#tweet_#{tweet._id}").removeClass("available").addClass("unable active").find(".t-error").html('正在重发')
        @selected = []
      error: (retval) ->
        console.log "error comment_weibos"
        console.log retval

  destroy_tweets: (_tweets)->
    $.ajax
      url: "destroy_tweets"
      type: "DELETE"
      data:
        tweets:  _tweets || @selected
        task:    @_id
      success: (retval) =>
        console.log "success delete weibos"
        for tweet in @selected
          $("#tweet_#{tweet._id}").removeClass("available").addClass("unable active").find(".t-error").html('已删除')
        @selected = []
      error: (retval) ->
        console.log "error comment_weibos"
        console.log retval

  clear_task: (actn)->
    $.ajax
      url: "clear_task"
      type: "DELETE"
      data:
        actn: actn
        task:    @_id
      success: (retval) =>
        console.log "success clear task"
        $s.alert(retval.msg.type, retval.msg.content)
      error: (retval) ->
        console.log "error comment_weibos"
        $s.alert(retval.msg.type, retval.msg.content)
        console.log retval

  show_comments: (_tweet)->
    $.ajax
      url: "get_comments"
      type: "GET"
      data:
        tid:  _tweet
      success: (retval) =>
        console.log retval
        if retval.error
          comments = []
        else
          comments = retval.value

        comments_html = ""
        for comment in comments
          comments_html += "<p class=\"comment-list\">
            <a href=\"#\" class=\"username\">#{comment.user.screen_name}</a>
            <span class=\"comment-time muted\">(#{comment.created_at})</span>
            <span>：</span>
            <br>
            <span class=\"comment-txt\">#{comment.text}</span>
          </p>"

        $('#comments_list').html(comments_html)
      error: (retval) ->
        console.log "error comment_weibos"
        console.log retval

  commit_comment: ->
    $.ajax
      url: "add_comment"
      type: "POST"
      data:
        tid:  $('#commit_tweet_id').val()
        comment: $('#comment_text').val()
      success: (retval) =>
        console.log retval
      error: (retval) ->
        console.log "error comment_weibos"
        console.log retval

window.task = new Task()

$(->

  $('.local-tag').click ->
    this_tag = $(this)
    if this_tag.html() != "全部"
      window.location.href = "#{window.location.pathname}?location=#{$(this_tag).attr('data')}"
    else
      window.location.href = "#{window.location.pathname}"

  $('.tag-txt-list ul').on 'click',('li'), () ->
    this_li = $(this)
    return if this_li.hasClass('unable')
    _td = $(this).attr('data').split('-')
    this_data =
      pindex:    _td[1]
      index:     _td[2]
      wid:       _td[3]
      uid:       _td[4]
      _id:       this_li.attr('id').split('_')[1]
      retweet:   true
      commented: false
    if this_li.hasClass("active")
      this_li.removeClass "active"
      this_li.find(".checkbox input").prop "checked", false
      task.remove_comment_tweet(this_data)
    else
      this_li.addClass "active"
      this_li.find(".checkbox input").prop "checked", true
      task.add_comment_tweet(this_data)

  $('#retweet-btn').click ->
    task.recomment()

  $('#delete-btn').click ->
    task.destroy_tweets()

  # $('.tag-txt-list ul .btn').click ->


  $('.delete-btn').click (e)->
    e.stopPropagation();
    _tli = $(this).closest('li')
    _tws = _tli.attr('id').split('_')[1]
    _td  = _tli.attr('data').split('-')
    this_data =
      pindex:    _td[1]
      index:     _td[2]
      wid:       _td[3]
      uid:       _td[4]
      _id:       _tws
      retweet:   true
      commented: false

    $("#tweet_#{_tws}").removeClass("available").addClass("unable active").find(".t-error").html('已删除')
    task.destroy_tweets({0:this_data})

  $('.select-all-btn').click ->
    $('.tag-txt-list li.active').click()
    $('.tag-txt-list li').click()

  $('.select-failed-btn').click ->
    $('.tag-txt-list li.active').click()
    $('.tag-txt-list li.lose').click()

  $('.select-suc-btn').click ->
    $('.tag-txt-list li.active').click()
    $('.tag-txt-list li:not(.lose)').click()

  $('.comment-btn').click ->
    _id = $(this).closest('li').attr('id').split('_')[1]
    $("#commit_tweet_id").val(_id)
    task.show_comments(_id)

  $('#commit_comment').click ->
    task.commit_comment()
    $("#myModal").modal('hide')
    false

  $('#clear_unclicked').click ->
    task.clear_task('unclicked')

  $('#clear_failed').click ->
    task.clear_task('failed')

  $('#clear_all').click ->
    task.clear_task('all')
)
